package com.canxi.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.canxi.common.core.domain.PageQuery;
import com.canxi.common.core.page.TableDataInfo;
import com.canxi.common.utils.StringUtils;
import com.canxi.user.domain.CxConfig;
import com.canxi.user.domain.bo.CxConfigBo;
import com.canxi.user.domain.vo.CxConfigVo;
import com.canxi.user.mapper.CxConfigMapper;
import com.canxi.user.service.IConfigService;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 微信配制Service业务层处理
 *
 * @author ruoyi
 * @date 2022-10-22
 */
@RequiredArgsConstructor
@Service
public class ConfigServiceImpl  extends JoinServiceImpl<CxConfigMapper,CxConfig>  implements IConfigService {


    @Autowired
    private final CxConfigMapper baseMapper;

    /**
     * 查询微信配制
     */
    @Override
    public CxConfigVo queryById(String id){
        return baseMapper.selectVoById(id);
    }


    /**
     * 查询微信配制列表
     */
    @Override
    public TableDataInfo<CxConfigVo> queryPageList(CxConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CxConfig> lqw = buildQueryWrapper(bo);
        Page<CxConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询微信配制列表
     */
    @Override
    public List<CxConfigVo> queryList(CxConfigBo bo) {
        LambdaQueryWrapper<CxConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CxConfig> buildQueryWrapper(CxConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CxConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getWid() != null, CxConfig::getWid, bo.getWid());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), CxConfig::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getAppid()), CxConfig::getAppid, bo.getAppid());
        lqw.eq(StringUtils.isNotBlank(bo.getSecret()), CxConfig::getSecret, bo.getSecret());
        lqw.eq(StringUtils.isNotBlank(bo.getToken()), CxConfig::getToken, bo.getToken());
        lqw.eq(StringUtils.isNotBlank(bo.getAeskey()), CxConfig::getAeskey, bo.getAeskey());
        lqw.eq(StringUtils.isNotBlank(bo.getIsPay()), CxConfig::getIsPay, bo.getIsPay());
        lqw.eq(StringUtils.isNotBlank(bo.getPartner()), CxConfig::getPartner, bo.getPartner());
        lqw.eq(StringUtils.isNotBlank(bo.getPartnerKey()), CxConfig::getPartnerKey, bo.getPartnerKey());
        lqw.eq(StringUtils.isNotBlank(bo.getIsused()), CxConfig::getIsused, bo.getIsused());
        lqw.eq(StringUtils.isNotBlank(bo.getRedirectUrl()), CxConfig::getRedirectUrl, bo.getRedirectUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getApivkey()), CxConfig::getApivkey, bo.getApivkey());
        lqw.eq(StringUtils.isNotBlank(bo.getLkeyPath()), CxConfig::getLkeyPath, bo.getLkeyPath());
        lqw.eq(StringUtils.isNotBlank(bo.getPrivateKeyPath()), CxConfig::getPrivateKeyPath, bo.getPrivateKeyPath());
        lqw.eq(StringUtils.isNotBlank(bo.getLprivateCertPath()), CxConfig::getLprivateCertPath, bo.getLprivateCertPath());
        lqw.eq(StringUtils.isNotBlank(bo.getPrivateCertPath()), CxConfig::getPrivateCertPath, bo.getPrivateCertPath());
        lqw.eq(StringUtils.isNotBlank(bo.getCertSerialNo()), CxConfig::getCertSerialNo, bo.getCertSerialNo());
        return lqw;
    }

    /**
     * 新增微信配制
     */
    @Override
    public Boolean insertByBo(CxConfigBo bo) {
        CxConfig add = BeanUtil.toBean(bo, CxConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改微信配制
     */
    @Override
    public Boolean updateByBo(CxConfigBo bo) {
        CxConfig update = BeanUtil.toBean(bo, CxConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CxConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除微信配制
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<CxConfigVo> listWxConfig() {
        JoinLambdaWrapper<CxConfig> wrapper = joinLambdaQueryWrapper(CxConfig.class);
//        wrapper.eq(Config::getType,"小程序")
//            .eq(Config::getIsused,"1");

        List<CxConfigVo> list = baseMapper.selectVoList(wrapper);
        return list;
    }

    /**获取配制列表*/
    @Override
    public List<CxConfigVo>ListWxConfigs(CxConfigBo configBo){

        JoinLambdaWrapper<CxConfig> wrapper = joinLambdaQueryWrapper(CxConfig.class);
        wrapper.equals(BeanUtil.toBean(configBo, CxConfig.class));
        List<CxConfigVo> list = baseMapper.selectVoList(wrapper);
        return list;


    }

}
